/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vita.vitasolution.service.api;

import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.persistence.*;
import javax.ws.rs.*;
import vita.vitasolution.db.beans.CaseDAO;
import vita.vitasolution.db.beans.UserDAO;
import vita.vitasolution.db.dictionaries.CaseState;
import vita.vitasolution.db.entities.Case;
import vita.vitasolution.db.entities.User;
import vita.vitasolution.service.dto.DetailedCase;
import vita.vitasolution.service.dto.ShortCase;
import vita.vitasolution.service.util.converters.CaseDetailedCaseConverter;
import vita.vitasolution.service.util.converters.CaseShortCaseConverter;

/**
 *
 * @author harker777
 */
@Stateless
@Path("api/cases")
public class CaseApi {

	@PersistenceContext(unitName = "VitaSolutionPU")
	private EntityManager em;
	@Inject
	private CaseShortCaseConverter caseShortCaseConverter;
	@Inject
	private CaseDetailedCaseConverter caseDetailedCaseConverter;
	@Inject
	private CaseDAO caseDAO;
	@Inject
	private UserDAO userDAO;

	@GET
	@Produces({"application/json"})
	public List<ShortCase> findAll(
			@QueryParam("from") int from,
			@QueryParam("length") int length,
			@QueryParam("orderBy") String orderBy) {
		List<Case> cases = caseDAO.findOrderedInterval(from, length, orderBy);

		List<ShortCase> shortCases = getShortCasesFromCases(cases);
		return shortCases;
	}

	@GET
	@Path("detailed")
	@Produces({"application/json"})
	public DetailedCase getDetailedCase(
			@QueryParam("id") int id) {
		Case theCase = caseDAO.findByID(id);
		DetailedCase detailedCase = caseDetailedCaseConverter.getDtoFromEntity(theCase);
		return detailedCase;
	}

	@POST
	@Path("create")
	@Consumes({"application/json"})
	@Produces({"application/json"})
	public DetailedCase createCase(DetailedCase detailedCase) {
		Case caseToPersist = caseDetailedCaseConverter.getEntityFromDto(detailedCase);
		caseDAO.create(caseToPersist);

		DetailedCase persistedCase = caseDetailedCaseConverter.getDtoFromEntity(caseToPersist);
		return persistedCase;
	}

	@POST
	@Path("update")
	@Consumes({"application/json"})
	@Produces({"application/json"})
	public DetailedCase updateCase(DetailedCase detailedCase) {
		Case caseToPersist;

		caseToPersist = caseDetailedCaseConverter.getEntityFromDto(detailedCase);
		caseToPersist.setCaseState(CaseState.OPENED);

		caseDAO.edit(caseToPersist);

		Case updatedCase = caseDAO.findByID(detailedCase.getId());
		return caseDetailedCaseConverter.getDtoFromEntity(updatedCase);
	}

	@GET
	@Path("opened")
	@Produces({"application/json"})
	public List<ShortCase> getOpenedCases(
			@QueryParam("userId") int userId) {
		User user = userDAO.findByID(userId);
		List<Case> cases = caseDAO.findCasesOfUserInState(user, CaseState.OPENED);

		List<ShortCase> result = new ArrayList<ShortCase>();

		for (Case aCase : cases) {
			result.add(caseShortCaseConverter.getDtoFromEntity(aCase));
		}

		return result;
	}

	@GET
	@Path("closed")
	@Produces({"application/json"})
	public List<ShortCase> getClosedCases(
			@QueryParam("userId") int userId) {
		User user = userDAO.findByID(userId);
		List<Case> cases = caseDAO.findCasesOfUserInState(user, CaseState.CLOSED);
		List<ShortCase> result = new ArrayList<ShortCase>();

		for (Case aCase : cases) {
			result.add(caseShortCaseConverter.getDtoFromEntity(aCase));
		}

		return result;
	}

	private List<ShortCase> getShortCasesFromCases(List<Case> cases) {
		List<ShortCase> shortCases = new ArrayList<ShortCase>();
		for (Case theCase : cases) {
			shortCases.add(caseShortCaseConverter.getDtoFromEntity(theCase));
		}
		return shortCases;
	}
}
